﻿
using IdentityModel.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace m.avis.cn
{
    /// <summary>
    /// Client端访问认识服务器
    /// </summary>
    public class CallIdentityServer
    {


        public static async Task GetTokenAsync()
        {
            string identityServerUrl = ConfigurationManager.AppSettings["IdentityServerURL"] ?? string.Empty;
            string clientName = ConfigurationManager.AppSettings["ClientName"] ?? string.Empty;
            string clientSecret = ConfigurationManager.AppSettings["ClientSecret"] ?? string.Empty;

            // 发现客户端
            var disco = await DiscoveryClient.GetAsync(identityServerUrl);

            // 请求令牌
            var tokenClient = new TokenClient(disco.TokenEndpoint, clientName, clientSecret);
            var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");

            //异步方法中不能使用Applciation对象，使用Static
            TokenCache.Token = tokenResponse.Json.ToString();

        }
    }
}